//
//  AutoLayoutOfLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/9.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "AutoLayoutOfLayerViewController.h"

@interface AutoLayoutOfLayerViewController ()

@property (nonatomic, strong) CALayer *blueLayer;
@property (nonatomic, assign) UIDeviceOrientation currentOriention; // 当前设备方向

@end

@implementation AutoLayoutOfLayerViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.blueLayer.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设备方向通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    self.currentOriention = UIDeviceOrientationPortrait;
    self.blueLayer = [[CALayer alloc] init];
    self.blueLayer.frame = CGRectMake(100, 100, 100, 100);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    self.blueLayer.delegate = self;
    [self.view.layer addSublayer:_blueLayer];

}

// 该方法在layer的bounds发生变化的时候会调用或者调用setNeesLayout的时候调用

- (void)layoutSublayersOfLayer:(CALayer *)layer
{
    NSLog(@"图层的bounds发生变化");
    if (self.currentOriention == UIDeviceOrientationPortrait || self.currentOriention == UIDeviceOrientationPortraitUpsideDown) {
        // 说明设备是竖向的
        self.blueLayer.frame = CGRectMake(100, 100, 100, 100);
    } else {
        // 说明设备是横向的
        self.blueLayer.frame = CGRectMake(50, 50, 380, 200);
    }
}

- (void)orientationChange:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case UIDeviceOrientationPortrait:
        {
            NSLog(@"设备正面");
            self.currentOriention = UIDeviceOrientationPortrait;
        //    self.blueLayer.frame = CGRectZero;
            [self.blueLayer setNeedsLayout];
            break;
        }
            case UIDeviceOrientationLandscapeLeft:
        {
            NSLog(@"设备左面");
            self.currentOriention = UIDeviceOrientationLandscapeLeft;
          // self.blueLayer.frame = CGRectZero;
             [self.blueLayer setNeedsLayout];
            break;
        }
            case UIDeviceOrientationLandscapeRight:
        {
            NSLog(@"设备右边");
            self.currentOriention = UIDeviceOrientationLandscapeRight;
          //  self.blueLayer.frame = CGRectZero;
             [self.blueLayer setNeedsLayout];
            break;
        }
            case UIDeviceOrientationPortraitUpsideDown:
        {
            NSLog(@"设备向下");
            self.currentOriention = UIDeviceOrientationPortraitUpsideDown;
          //  self.blueLayer.frame = CGRectZero;
             [self.blueLayer setNeedsLayout];
            break;
        }
        default:
            break;
    }
}

@end
